home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’92 / NetWarmer / source / DialogAids.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-11  |  11.7 KB  |  564 lines  |  [TEXT/KAHL]

  1. /* © 1988, Bowers Development Corp. */
  2. /* DialogAids.c */
  3.  
  4. #include "DoScrap.h"
  5. #include "Scrolling.h"
  6. #include "WindowAids.h"
  7.  
  8. #include "DialogAids.h"
  9.  
  10.  
  11. /*----------*/
  12. pascal void LineItem    (dialog, itemNr)
  13. DialogPtr    dialog;
  14. short        itemNr;
  15. {
  16.     short            itemType;
  17.     Handle            item;
  18.     Rect            itemRect;
  19.     PenState        savePen;
  20.  
  21.     GetDItem (dialog, itemNr, &itemType, &item, &itemRect);
  22.     GetPenState (&savePen);
  23.     PenNormal ();
  24.     PenPat (gray);
  25.     MoveTo (itemRect.left, itemRect.top);
  26.     LineTo (itemRect.right - 1, itemRect.bottom - 1);
  27.     SetPenState (&savePen);
  28. } /*LineItem*/
  29.  
  30. /*----------*/
  31. pascal void RectItem    (dialog, itemNr)
  32. DialogPtr    dialog;
  33. short        itemNr;
  34. {
  35.     short            itemType;
  36.     Handle            item;
  37.     Rect            itemRect;
  38.     PenState        savePen;
  39.  
  40.     GetDItem (dialog, itemNr, &itemType, &item, &itemRect);
  41.     GetPenState (&savePen);
  42.     PenNormal ();
  43.     FrameRect (&itemRect);
  44.     SetPenState (&savePen);
  45. } /*RectItem*/
  46.  
  47. /*----------*/
  48. void SetUserItem    (itemNr, doDraw)
  49. short        itemNr;
  50. ProcPtr        doDraw;
  51. {
  52.     short            itemType;
  53.     Handle            item;
  54.     Rect            itemRect;
  55.  
  56.     GetDItem (thePort, itemNr, &itemType, &item, &itemRect);
  57.     SetDItem (thePort, itemNr, itemType, (Handle) doDraw, &itemRect);
  58. } /*SetUserItem*/
  59.  
  60. /*----------*/
  61. void OutlineButton    (itemNr)
  62. short            itemNr;
  63. {
  64.     short            itemType;
  65.     Handle            item;
  66.     Rect            itemRect;
  67.     PenState        savePen;
  68.  
  69.     GetDItem (thePort, itemNr, &itemType, &item, &itemRect);
  70.     GetPenState (&savePen);
  71.     PenNormal ();
  72.     PenSize (3, 3);
  73.     InsetRect (&itemRect, -4, -4);
  74.     FrameRoundRect (&itemRect, 16, 16);
  75.     SetPenState (&savePen);
  76. } /*OutlineButton*/
  77.  
  78. /*----------*/
  79. void EnableDItem    (itemNr, enable)
  80. short        itemNr;
  81. Boolean        enable;
  82. {
  83.     short            itemType;
  84.     Handle            item;
  85.     Rect            itemRect;
  86.     ControlHandle    control;
  87.  
  88.     GetDItem (thePort, itemNr, &itemType, &item, &itemRect);
  89.     itemType = itemType & ~itemDisable;
  90.     SetDItem (thePort, itemNr, itemType, item, &itemRect);
  91.     control = (ControlHandle) item;
  92.     HiliteScroll (control, enable);
  93. } /*EnableDItem*/
  94.  
  95. /*----------*/
  96. void SetDText        (itemNr, text)
  97. short        itemNr;
  98. Str255        text;
  99. {
  100.     short            itemType;
  101.     Handle            item;
  102.     Rect            itemRect;
  103.  
  104.     GetDItem (thePort, itemNr, &itemType, &item, &itemRect);
  105.     SetIText (item, text);
  106. } /*SetDText*/
  107.  
  108. /*----------*/
  109. void GetDText        (itemNr, text)
  110. short        itemNr;
  111. Str255        text;
  112. {
  113.     short            itemType;
  114.     Handle            item;
  115.     Rect            itemRect;
  116.  
  117.     GetDItem (thePort, itemNr, &itemType, &item, &itemRect);
  118.     GetIText (item, text);
  119. } /*GetDText*/
  120.  
  121. /*----------*/
  122. void SetDNum        (itemNr, num)
  123. short        itemNr;
  124. long        num;
  125. {
  126.     Str255            text;
  127.  
  128.     NumToString (num, text);
  129.     SetDText (itemNr, text);
  130. } /*SetDNum*/
  131.  
  132. /*----------*/
  133. void GetDNum        (itemNr, num)
  134. short        itemNr;
  135. long        *num;
  136. {
  137.     Str255            text;
  138.  
  139.     GetDText (itemNr, text);
  140.     StringToNum (text, num);
  141. } /*GetDNum*/
  142.  
  143. /*----------*/
  144. void SetCheckbox    (itemNr, checked)
  145. short        itemNr;
  146. Boolean        checked;
  147. {
  148.     short            itemType;
  149.     Handle            item;
  150.     Rect            itemRect;
  151.     ControlHandle    control;
  152.  
  153.     GetDItem (thePort, itemNr, &itemType, &item, &itemRect);
  154.     control = (ControlHandle) item;
  155.     SetCtlMax (control, 1);
  156.     SetCtlValue (control, checked);
  157. } /*SetCheckbox*/
  158.  
  159. /*----------*/
  160. void DoCheckbox        (itemNr, checked)
  161. short        itemNr;
  162. Boolean        *checked;
  163. {
  164.     *checked = !*checked;
  165.     SetCheckbox (itemNr, *checked);
  166. } /*DoCheckbox*/
  167.  
  168. /*----------*/
  169. void SetRadio        (firstItem, choice)
  170. short        firstItem;
  171. short        choice;
  172. {
  173.     SetCheckbox (firstItem + (choice - 1), true); 
  174. } /*SetRadio*/
  175.  
  176. /*----------*/
  177. void DoRadio        (firstItem, itemNr, choice)
  178. short        firstItem;
  179. short        itemNr;
  180. short        *choice;
  181. {
  182.     SetCheckbox (firstItem + (*choice - 1), false);
  183.     *choice = itemNr - firstItem + 1;
  184.     SetCheckbox (firstItem + (*choice - 1), true); 
  185. } /*DoRadio*/
  186.  
  187. /*----------*/
  188. void InvertIcon (short      iconNr);
  189. void InvertIcon (iconNr)
  190. short        iconNr;
  191. {
  192.     short            itemType;
  193.     Handle            item;
  194.     Rect            itemRect;
  195.  
  196.     GetDItem (thePort, iconNr, &itemType, &item, &itemRect);
  197.     InsetRect (&itemRect, 1, 1);
  198.     InvertRect (&itemRect);
  199. } /*InvertIcon*/
  200.  
  201. /*----------*/
  202. void DoIconRadio    (firstIcon, itemNr, choice)
  203. short        firstIcon;
  204. short        itemNr;
  205. short        *choice;
  206. {
  207.     InvertIcon (firstIcon + (*choice - 1));
  208.     *choice = (itemNr - firstIcon) + 1;
  209.     InvertIcon (firstIcon + (*choice - 1));
  210. } /*DoIconRadio*/
  211.  
  212. /*----------*/
  213. void SetControl        (itemNr, choice)
  214. short        itemNr;
  215. short        choice;
  216. {
  217.     short            itemType;
  218.     Handle            item;
  219.     Rect            itemRect;
  220.     ControlHandle    control;
  221.  
  222.     GetDItem (thePort, itemNr, &itemType, &item, &itemRect);
  223.     control = (ControlHandle) item;
  224.     SetCtlValue (control, choice);
  225. } /*SetControl*/
  226.  
  227. /*----------*/
  228. short GetControl        (itemNr)
  229. short        itemNr;
  230. {
  231.     short            itemType;
  232.     Handle            item;
  233.     Rect            itemRect;
  234.     ControlHandle    control;
  235.  
  236.     GetDItem (thePort, itemNr, &itemType, &item, &itemRect);
  237.     control = (ControlHandle) item;
  238.     return (GetCtlValue (control));
  239. } /*GetControl*/
  240.  
  241. /*----------*/
  242. void DoPalette        (itemNr, choice)
  243. short        itemNr;
  244. short        *choice;
  245. {
  246.     *choice = GetControl (itemNr);
  247. } /*DoPalette*/
  248.  
  249. /*----------*/
  250. void DoMultiState    (itemNr, value)
  251. short         itemNr;
  252. short        *value;
  253. {
  254.     short            itemType;
  255.     Handle            item;
  256.     Rect            itemRect;
  257.     ControlHandle    control;
  258.  
  259.     GetDItem (thePort, itemNr, &itemType, &item, &itemRect);
  260.     control = (ControlHandle) item;
  261.     if (*value == GetCtlMax (control)) {
  262.         *value = GetCtlMin (control);
  263.     } else {
  264.         *value += 1;
  265.     }
  266.     SetCtlValue (control, *value);
  267. } /*DoMultiState*/
  268.  
  269. /*----------*/
  270. void SetScrollItem  (itemNr, value, min, max, pageSize)
  271. short        itemNr;
  272. short        value;
  273. short        min;
  274. short        max;
  275. short        pageSize;
  276. {
  277.     short            itemType;
  278.     Handle            item;
  279.     Rect            itemRect;
  280.     ControlHandle    scroll;
  281.  
  282.     GetDItem (thePort, itemNr, &itemType, &item, &itemRect);
  283.     scroll = (ControlHandle) item;
  284.     SetCtlMin     (scroll, min);
  285.     SetCtlMax     (scroll, max);
  286.     SetCtlValue     (scroll, value);
  287.     SetCRefCon     (scroll, pageSize);
  288.     HiliteScroll (scroll, (min < max));
  289. } /*SetScrollItem*/
  290.  
  291. /*----------*/
  292. void DoScrollItem    (itemNr, value)
  293. short        itemNr;
  294. short        *value;
  295. {
  296.     short            itemType;
  297.     Handle            item;
  298.     Rect            itemRect;
  299.     ControlHandle    scroll;
  300.  
  301.     GetDItem (thePort, itemNr, &itemType, &item, &itemRect);
  302.     scroll = (ControlHandle) item;
  303.     *value = GetCtlValue (scroll);
  304. } /*DoScrollItem*/
  305.  
  306. /*----------*/
  307. void DrawPopup        (itemNr, menuID, choice)
  308. short        itemNr;
  309. short        menuID;
  310. short        choice;
  311. {
  312.     short            itemType;
  313.     Handle            item;
  314.     Rect            itemRect;
  315.  
  316.     GetDItem (thePort, itemNr, &itemType, &item, &itemRect);
  317.     UpdatePopup (itemRect, menuID, choice);
  318. } /*DrawPopup*/
  319.  
  320. /*----------*/
  321. void DoPopup        (itemNr, menuID, choice)
  322. short        itemNr;
  323. short        menuID;
  324. short        *choice;
  325. {
  326.     short            itemType;
  327.     Handle            item;
  328.     Rect            itemRect;
  329.  
  330.     GetDItem (thePort, itemNr, &itemType, &item, &itemRect);
  331.     TrackPopup (itemRect, menuID, choice);
  332. } /*DoPopup*/
  333.  
  334. /*----------*/
  335. void InvertLabel    (itemNr)
  336. short        itemNr;
  337. {
  338.     short            itemType;
  339.     Handle            item;
  340.     Rect            itemRect;
  341.  
  342.     GetDItem (thePort, itemNr, &itemType, &item, &itemRect);
  343.     InvertRect (&itemRect);
  344. } /*InvertLabel*/
  345.  
  346. /*----------*/
  347. ListHandle  Vert1List    (itemNr)
  348. short        itemNr;
  349. {
  350.     short            itemType;
  351.     Handle            item;
  352.     Rect            itemRect;
  353.     Rect            dataBounds;
  354.     Point            cSize;
  355.     ListHandle        list;
  356.  
  357.     GetDItem (thePort, itemNr, &itemType, &item, &itemRect);
  358.     itemRect.right = itemRect.right - 15;    /*for scroll bar*/
  359.     SetRect (&dataBounds, 0, 0, 1, 0);    /*one column, no rows*/
  360.     SetPt (&cSize, itemRect.right - itemRect.left, 0);
  361.     list = LNew (&itemRect,        /*dialog item's rect*/
  362.                  &dataBounds,    /*one column, no rows*/
  363.                  cSize,            /*cell size: full width, standard height*/
  364.                  0,                /*procid - standard text list*/
  365.                  thePort,        /*parent window*/
  366.                  false,            /*don't draw*/
  367.                  false,            /*has grow*/
  368.                  false,            /*no horizontal scroll*/
  369.                  true);            /*vertical scroll*/
  370.     return (list);
  371. } /*Vert1List*/
  372.  
  373. /*----------*/
  374. void AddToList        (data, list)
  375. char            data [];
  376. ListHandle        list;
  377. {
  378.     short            newRow;
  379.     Point            newCell;
  380.     unsigned char    dataLen;
  381.     
  382.     #define maxint    32767
  383.  
  384.     newRow = LAddRow (1, maxint, list);
  385.     SetPt (&newCell, 0, newRow);
  386.     dataLen = data [0];
  387.     LSetCell (&(data [1]), dataLen, newCell, list);
  388. } /*AddToList*/
  389.  
  390. /*----------*/
  391. void DrawList (list)
  392. ListHandle        list;
  393. {
  394.     PenState        savePen;
  395.     Rect            frame;
  396.  
  397.     GetPenState (&savePen);
  398.     PenNormal ();
  399.     frame = (**list).rView;
  400.     InsetRect (&frame, -1, -1);
  401.     FrameRect (&frame);
  402.     SetPenState (&savePen);
  403.     LUpdate ((**list).port->visRgn, list);
  404. } /*DrawList*/
  405.  
  406. /*----------*/
  407. Boolean FilterList (event, list, listItem, dblClickItem, itemHit)
  408. EventRecord        *event;
  409. ListHandle        list;
  410. short            listItem;
  411. short            dblClickItem;
  412. short            *itemHit;
  413. {
  414.     Boolean            filtered;
  415.     Point            mousePos;
  416.  
  417.     filtered = false;
  418.     if ((*event).what == mouseDown) {
  419.         mousePos = (*event).where;
  420.         GlobalToLocal (&mousePos);
  421.         if (FindDItem (thePort, mousePos) + 1 == listItem) {
  422.             if (LClick (mousePos, (*event).modifiers, list)) {
  423.                 *itemHit = dblClickItem;
  424.             } else {
  425.                 *itemHit = listItem;
  426.             }
  427.             filtered = true;
  428.         }
  429.     }
  430.     return (filtered);
  431. } /*FilterList*/
  432.  
  433. /*----------*/
  434. Boolean FilterScroll (event, scrollItem, actionProc, itemHit)
  435. EventRecord        *event;
  436. short            scrollItem;
  437. ProcPtr            actionProc;
  438. short            *itemHit;
  439. {
  440.     Boolean            filtered;
  441.     Point            mousePos;
  442.     short            partCode;
  443.     ControlHandle    whichControl;
  444.  
  445.     filtered = false;
  446.     if ((*event).what == mouseDown) {
  447.         mousePos = (*event).where;
  448.         GlobalToLocal (&mousePos);
  449.         if (FindDItem (thePort, mousePos) + 1 == scrollItem) {
  450.             partCode = FindControl (mousePos, thePort, &whichControl);
  451.             if (partCode != 0) {
  452.                 TrackScroll (whichControl, partCode, mousePos, actionProc);
  453.                 *itemHit = scrollItem;
  454.                 filtered = true;
  455.             }
  456.         }
  457.     }
  458.     return (filtered);
  459. } /*FilterScroll*/
  460.  
  461. /*----------*/
  462. void FilterIcon    (event, firstIcon, choice)
  463. EventRecord        *event;
  464. short            firstIcon;
  465. short            choice;
  466. {
  467.     short            itemType;
  468.     Handle            item;
  469.     Rect            itemRect;
  470.  
  471.     if (((*event).what == updateEvt)
  472.     &&  ((WindowPtr) (*event).message == thePort)) {
  473.         GetDItem (thePort, firstIcon + (choice - 1), &itemType, &item, &itemRect);
  474.         PlotIcon (&itemRect, item);
  475.         ValidRect (&itemRect);
  476.         InsetRect (&itemRect, 1, 1);
  477.         InvertRect (&itemRect);
  478.     }
  479. } /*FilterIcon*/
  480.  
  481. /*----------*/
  482. pascal Boolean StandardFilter (whichDialog, event, itemHit)
  483. DialogPtr        whichDialog;
  484. EventRecord        *event;
  485. short            *itemHit;
  486. {
  487.     #define returnKey        13
  488.     #define enter            3
  489.     #define esc                27
  490.     #define pushButton        (ctrlItem + btnCtrl)
  491.     
  492.     Boolean            filtered;
  493.     DialogPeek        whichPeek;
  494.     short            defItem;
  495.     char            key;
  496.     short            itemType;
  497.     Handle            item;
  498.     Rect            itemRect;
  499.     long            finalTicks;
  500.  
  501.     filtered = false;
  502.     whichPeek = (DialogPeek) whichDialog;
  503.     defItem = whichPeek->aDefItem;
  504.     switch ((*event).what) {
  505.     case keyDown:
  506.     case autoKey:
  507.         key = (*event).message & charCodeMask;
  508.         if ((key == returnKey) || (key == enter) || (key == esc)) {
  509.             if ((key == returnKey) || (key == enter)) {
  510.                 *itemHit = defItem;
  511.             } else if (key == esc) {
  512.                 *itemHit = Cancel;
  513.             }
  514.             GetDItem (whichDialog, *itemHit, &itemType, &item, &itemRect);
  515.             if ((itemType & itemDisable) == 0) {
  516.                 if ((itemType & (255 - itemDisable)) == pushButton) {
  517.                     HiliteControl ((ControlHandle) item, inButton);
  518.                     Delay (8, &finalTicks);
  519.                     HiliteControl ((ControlHandle) item, 0);
  520.                 }
  521.                 filtered = true;
  522.             } /*if*/
  523.         } else {
  524.             if ((((*event).modifiers & cmdKey) != 0)
  525.             &&  ((key == 'x') || (key == 'c') || (key == 'v'))) {
  526.                 *itemHit = (whichPeek)->editField + 1;
  527.                 switch (key) {
  528.                 case 'x':
  529.                         DlgCut (whichDialog);
  530.                         scrapDirty = (*itemHit > 0);
  531.                     break;
  532.                 case 'c':
  533.                         DlgCopy (whichDialog);
  534.                         scrapDirty = (*itemHit > 0);
  535.                         *itemHit = 0;
  536.                     break;
  537.                 case 'v':
  538.                         DlgPaste (whichDialog);
  539.                     break;
  540.                 } /*switch*/
  541.                 if (*itemHit > 0) {
  542.                     GetDItem (whichDialog, *itemHit, &itemType, &item, &itemRect);
  543.                     if ((itemType & itemDisable) == 0) {
  544.                         filtered = true;
  545.                     }
  546.                 }
  547.                 if (!filtered) {
  548.                     (*event).what = nullEvent;
  549.                 }
  550.             }
  551.         }
  552.         break;
  553.     case updateEvt:
  554.         if ((WindowPtr) (*event).message == thePort) {
  555.             GetDItem (whichDialog, defItem, &itemType, &item, &itemRect);
  556.             if ((itemType & (255 - itemDisable)) == pushButton) {
  557.                 OutlineButton (defItem);
  558.             }
  559.         }
  560.         break;
  561.     } /*switch*/
  562.     return (filtered);
  563. } /*StandardFilter*/
  564.